package com.mxf.example.utils.http;

import com.mxf.example.exception.ResultEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;


/**
 * @Description http请求结果类
 * @Author mxf
 * @Date 2021/3/26 14:41
 * @Param
 * @Return
 * @Exception
 */
@Data
public class Result<T> {

    /**
     * 成功数据
     */
    @ApiModelProperty(value = "响应数据体")
    private T data;

    /**
     * 响应编码200为成功
     */
    @ApiModelProperty(value = "响应状态码", example = "200")
    private int code;

    /**
     * 描述
     */
    @ApiModelProperty(value = "响应消息", example = "ok")
    private String msg;


    public Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static Result create(Integer code, String msg) {
        Result result = new Result(code, msg);
        return result;
    }

    /**
     * 无数据返回成功
     *
     * @return
     */
    public static Result createSuccess() {
        return create(ResultEnum.OK.getResultCode(), ResultEnum.OK.name());
    }

    /**
     * 有数据返回成功
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result<T> createSuccess(T data) {
        return createSuccess().setData(data);
    }

    /**
     * 有数据自定义返回
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result<T> createSuccess(T data, Integer code, String msg) {
        return create(code, msg).setData(data);
    }

    /**
     * 无描述返回失败
     *
     * @return
     */
    public static Result createFail() {
        return create(ResultEnum.INTERNAL_SERVER_ERROR.getResultCode(), ResultEnum.INTERNAL_SERVER_ERROR.getResultMsg());
    }

    /**
     * 自定义返回失败描述
     *
     * @param code
     * @param msg
     * @return
     */
    public static Result createFail(Integer code, String msg) {
        return create(code, msg);
    }

    public Result setData(T data) {
        this.data = data;
        return this;
    }

}
